package com.example.administrator.mobilesafe.utils;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Administrator on 2015/9/1.
 */
public class NumberAddressQuery {
    private static String path = "data/data/com.example.administrator.mobliesafe/files/address.db";
    public static String queryNumberAddress(String number){
        String address = null;
        SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        if(number.matches("^1[34568]\\d{9}$")){
            //把address.db拷贝到我们的/data/data/包名/files/address.db
            Cursor cursor = sqLiteDatabase.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)",
                    new String[]{number.substring(0, 7)});
            while (cursor.moveToNext()){
                address = cursor.getString(0);
            }
            cursor.close();
            sqLiteDatabase.close();
            return address;
        }else {
            switch (number.length()){
                case 3:
                    address = "匪警号码";
                    break;
                case 4:
                    address = "模拟器";
                    break;
                case 5:
                    address = "客服电话";
                    break;
                case 6:
                    address = "客服电话";
                    break;
                case 7:
                    address = "本地号码";
                    break;
                case 8:
                    address = "本地号码";
                    break;
                default:
                    if(number.length()>10&&number.startsWith("0")){
                        Cursor cursor = sqLiteDatabase.rawQuery("select location from data2 where area = ?",new String[]{number.substring(1,3)});
                        while (cursor.moveToNext()){
                            address = cursor.getString(0);
                        }
                        cursor.close();
                        cursor = sqLiteDatabase.rawQuery("select location from data2 where area = ?",new String[]{number.substring(1,4)});
                        while (cursor.moveToNext()){
                            address = cursor.getString(0);
                        }
                        cursor.close();
                        sqLiteDatabase.close();
                    }
                    break;

            }
            return address;
        }
    }
}
